package ru.mirent.url;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import ru.mirent.url.UrlFormatSchemeHostPortPath;

import java.util.stream.Stream;

public class UrlFormatSchemeHostPortPathTest {

    @Test
    void positiveDefaultPathTest() {
        String actualUrl = Assertions.assertDoesNotThrow(
                () -> new UrlFormatSchemeHostPortPath().apply("https://www.google.com/search")
        );

        Assertions.assertEquals("https://www.google.com/search", actualUrl);
    }

    @Test
    void positiveSetPathTest() {
        String actualUrl = Assertions.assertDoesNotThrow(
                () -> new UrlFormatSchemeHostPortPath("/").apply("https://www.google.com/search")
        );

        Assertions.assertEquals("https://www.google.com/search", actualUrl);
    }

    @Test
    void positiveSetEmptyPathTest() {
        String actualUrl = Assertions.assertDoesNotThrow(
                () -> new UrlFormatSchemeHostPortPath("").apply("https://www.google.com/search")
        );

        Assertions.assertEquals("https://www.google.com/search", actualUrl);
    }

    @Test
    void positiveSetEmptyPath1Test() {
        String actualUrl = Assertions.assertDoesNotThrow(
                () -> new UrlFormatSchemeHostPortPath("").apply("https://www.google.com/")
        );

        Assertions.assertEquals("https://www.google.com", actualUrl);
    }

    @ParameterizedTest
    @ArgumentsSource(PositiveTestData.class)
    void positiveTest(String url, String expectedUrl) {
        String actualUrl = Assertions.assertDoesNotThrow(
                () -> new UrlFormatSchemeHostPortPath().apply(url)
        );

        Assertions.assertEquals(expectedUrl, actualUrl);
    }

    private static class PositiveTestData implements ArgumentsProvider {
        @Override
        public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
            return Stream.of(
                    Arguments.of("https://www.google.com/search?sca_esv=f4da&sca_upv=1&q=Observer+pattern#imgrc=zVL2bLIFK12jZM",
                            "https://www.google.com/search"),
                    Arguments.of("https://www.google.com:9080/search", "https://www.google.com:9080/search"),
                    Arguments.of("https://www.google.com:9080", "https://www.google.com:9080/"),
                    Arguments.of("https://www.google.com", "https://www.google.com/")
            );
        }
    }
}
